package com.trw.trp.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.trw.trp.bean.ModelBean;
import com.trw.trp.bean.OrderBean;
import com.trw.trp.bean.PropertyBean;
import com.trw.trp.bean.SearchCriteriaBean;
import com.trw.trp.dao.DaoFactory;
import com.trw.trp.dao.OrderDao;
import com.trw.trp.dao.SystemSettingDao;
import com.trw.trp.msg.ResultMessage;
import com.utuan.log.Log;
import com.utuan.log.LogFactory;

public class UpgradeAction extends AbstractAction {
	private static Log log = LogFactory.getLog(AbstractAction.class);

	public void upgrade20121024() throws IOException {
		ResultMessage ret = new ResultMessage();
		try {
			SystemSettingDao dao = DaoFactory.getSystemSettingDao();
			PropertyBean prop = dao.getProperty(5L);
			String dirStr = prop.getValue();
			Calendar cal = new GregorianCalendar();
			cal.setTime(new java.util.Date());
			cal.add(Calendar.MONTH, -2);
			SearchCriteriaBean criteria = new SearchCriteriaBean();
			criteria.setStartTime(cal.getTime());
			OrderDao dao2 = DaoFactory.getOrderDao();
			List<OrderBean> orderList = dao2.getOrders(criteria);
			for (OrderBean order : orderList) {
				String dir1 = dirStr + File.separator + order.getOrderNumber();
				log.info("process dir:" + dir1);
				int count = 0;
				File file = new File(dir1);
				if (file.exists()) {
					String[] fileNames = file.list();
					if (fileNames != null) {
						for (String fileName : fileNames) {
							if ("generalInfo".equals(fileName)) {
								count++;
							}
							if ("generalInfo2".equals(fileName)) {
								count++;
							}
						}
					}
				}
				if (count == 2) {
					log.info("need process request:" + dir1);
					order = dao2.getOrder(order.getOrderId());
					List<ModelBean> modelList = order.getModelMap().get(1);
					for (ModelBean model : modelList) {
						String newFileDir = dir1 + File.separator + "techinfo_353_" + model.getClientUniqueId();
						String newFileDir2 = dir1 + File.separator + "techinfo_355_" + model.getClientUniqueId();
						copyDirectory(new File(dir1 + File.separator + "generalInfo"), new File(newFileDir));
						copyDirectory(new File(dir1 + File.separator + "generalInfo2"), new File(newFileDir2));
					}
				}
			}
		} catch (Exception e) {
			ret.setState(ResultMessage.FAILED);
			ret.setMessage(e.getMessage());
		}
		writeJSON(ret);
	}

	public static final void copyDirectory(File source, File destination) throws IOException {
		if (!source.isDirectory()) {
			throw new IllegalArgumentException("Source (" + source.getPath() + ") must be a directory.");
		}

		if (!source.exists()) {
			throw new IllegalArgumentException("Source directory (" + source.getPath() + ") doesn't exist.");
		}

		if (destination.exists()) {
			throw new IllegalArgumentException("Destination (" + destination.getPath() + ") exists.");
		}

		destination.mkdirs();
		File[] files = source.listFiles();

		for (File file : files) {
			if (file.isDirectory()) {
				copyDirectory(file, new File(destination, file.getName()));
			} else {
				copyFile(file, new File(destination, file.getName()));
			}
		}
	}

	public static final void copyFile(File source, File destination) throws IOException {
		FileChannel sourceChannel = new FileInputStream(source).getChannel();
		FileChannel targetChannel = new FileOutputStream(destination).getChannel();
		sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
		sourceChannel.close();
		targetChannel.close();
	}

}
